package com.tjtt.tdm.system.dao.impl;
import java.util.Map;

import org.hibernate.Query;

import com.tjtt.tdm.base.HibernateEntityDAO;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.system.bean.LogInfo;
import com.tjtt.tdm.system.dao.LogDao;

public class LogDaoImpl extends HibernateEntityDAO<LogInfo> implements LogDao {
    //查询所有日志信息
	public PageResult queryLogList(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from SYS_LOG where 1=1 ");
		queryString.append("select LOG_ID,LOG_ORG_NAME,LOG_USER_NAME,LOG_TYPE,LOG_MODULE_NAME,LOG_LOGIN_EVENT,LOG_LOGIN_IP, to_char(LOG_CREATTIME,'yyyy-MM-dd hh24:mi:ss') from SYS_LOG where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("logModuleName") &&! "".equals(filterMap.get("logModuleName"))){
				queryString.append("and LOG_MODULE_NAME like '%" +  filterMap.get("logModuleName") + "%' ");
				queryCountString.append("and LOG_MODULE_NAME like '%" +  filterMap.get("logModuleName") + "%' ");
			}
			if(null != filterMap.get("logOrgName") &&! "".equals(filterMap.get("logOrgName"))){
				queryString.append("and LOG_ORG_NAME like '%" + filterMap.get("logOrgName") + "%'  ");
				queryCountString.append("and LOG_ORG_NAME like '%" + filterMap.get("logOrgName") + "%'  ");
			}
			if(null != filterMap.get("logUserName") &&! "".equals(filterMap.get("logUserName"))){
				queryString.append("and LOG_USER_NAME like '%" + filterMap.get("logUserName") + "%' ");
				queryCountString.append("and LOG_USER_NAME like '%" + filterMap.get("logUserName") + "%' ");
			}
//			String ip= (String) filterMap.get("logLoginIp");
//			System.out.println( ip);
			// 界面没有IP的输入框
//			if(null != filterMap.get("logLoginIp") &&! "".equals(filterMap.get("logLoginIp"))){
//				queryString.append("and LOG_LOGIN_IP like '%" + filterMap.get("logLoginIp") + "%' ");
//			}
			if(null != filterMap.get("logType") &&! "".equals(filterMap.get("logType"))){
				queryString.append("and LOG_TYPE like '%" + filterMap.get("logType") + "%' ");
				queryCountString.append("and LOG_TYPE like '%" + filterMap.get("logType") + "%' ");
			}
			if(null != filterMap.get("logStarTime") &&! "".equals(filterMap.get("logStarTime"))){
				queryString.append("and to_char(LOG_CREATTIME,'yyyy-MM-dd') >= " + "'" + filterMap.get("logStarTime").toString() + "' ");
				queryCountString.append("and to_char(LOG_CREATTIME,'yyyy-MM-dd') >= " + "'" + filterMap.get("logStarTime").toString() + "' ");
			}
			if(null != filterMap.get("logEndTime") &&! "".equals(filterMap.get("logEndTime"))){
				queryString.append("and to_char(LOG_CREATTIME,'yyyy-MM-dd') <= " + "'" + filterMap.get("logEndTime").toString() + "' ");
				queryCountString.append("and to_char(LOG_CREATTIME,'yyyy-MM-dd') <= " + "'" + filterMap.get("logEndTime").toString() + "' ");
			}
			if(null != filterMap.get("userid") &&! "".equals(filterMap.get("userid"))){
				if("1".equals(filterMap.get("userid"))){//如果是系统管理员登陆  用户角色之外 归 系统管理员
					queryString.append("and LOG_USER_ID not in(1,2,3,4) ");
					queryCountString.append("and LOG_USER_ID not in(1,2,3,4) ");
				}else if("2".equals(filterMap.get("userid"))){//安全保密员 查看 安全审计和系统管理员 操作日志
					queryString.append("and LOG_USER_ID in(1,3) ");
					queryCountString.append("and LOG_USER_ID in(1,3) ");
				}else if("3".equals(filterMap.get("userid"))){
					queryString.append("and LOG_USER_ID in(1,2) ");
					queryCountString.append("and LOG_USER_ID in(1,2) ");
				}
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
    //通过主键id查询日志信息
	public LogInfo getLogById(long logId) {
		LogInfo logInfo=(LogInfo) super.getHibernateTemplate().get(LogInfo.class ,logId);
		 return logInfo ;
	}
	//定时删除日志
	public void delLogInfo(int sysLogsaveDay) throws DaoException {
		String sql = "delete from (select t.log_id, t.day " +
				"from (select l.log_id,round(to_number(to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') " +
				"- to_date(to_char(l.log_creattime,'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24:mi:ss'))) as day from sys_log l) t where t.day > "+sysLogsaveDay+")";
		this.executeSQL(sql);
	}
	@Override
	public void deleteLogAll() {
		String hql = "delete from LogInfo";
		Query query = getSession().createQuery(hql); 
		query.executeUpdate();
	}

}
